﻿Public Class masFormula
    Private STATUS = Nothing

    Private FMID = Nothing
    Private FMNAME = Nothing

    Private DFMID = Nothing
    Private L_MARGIN = Nothing
    Private H_MARGIN = Nothing
    Private FORMULA = Nothing

    Private selectMasformula = "SELECT `fmid` as 'รหัสสูตร', `fmname` as 'ชื่อสูตร', `status` as 'สถานะการเปิดใช้งาน' FROM masformula"
    Private selectMasdformula = "SELECT a.`fmid` AS 'รหัสสูตร', b.`fmname` AS 'ชื่อสูตร', a.`dfmid` AS 'รหัสสูตรคำนวณ', a.`formula` AS 'สูตรคำนวณ', a.`l_margin` AS 'ต่ำสุด', a.`h_margin` AS 'สูงสุด', a.`status` AS 'สถานะการเปิดใช้งาน' FROM masdformula a JOIN masformula b ON a.`fmid` = b.`fmid`"
    'Private selectMasdrgdoswuse = "SELECT USEID, DUCODE, DUSENAME, STATUS FROM masdrgdoswuse"
    'Private selectMasdrgacc = "SELECT DGTYID, DRGTYPE, convert(bit, [STATUS]) FROM MASDRGTYPE"

    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Property FMID_ As Integer
        Get
            Return FMID
        End Get
        Set(value As Integer)
            FMID = value
        End Set
    End Property

    Property FMNAME_ As String
        Get
            Return FMNAME
        End Get
        Set(value As String)
            FMNAME = value
        End Set
    End Property

    Property DFMID_ As Integer
        Get
            Return DFMID
        End Get
        Set(value As Integer)
            DFMID = value
        End Set
    End Property

    Property L_MARGIN_ As Double
        Get
            Return L_MARGIN
        End Get
        Set(value As Double)
            L_MARGIN = value
        End Set
    End Property

    Property H_MARGIN_ As Double
        Get
            Return H_MARGIN
        End Get
        Set(value As Double)
            H_MARGIN = value
        End Set
    End Property

    Property FORMULA_ As String
        Get
            Return FORMULA
        End Get
        Set(value As String)
            FORMULA = value
        End Set
    End Property

    Property STATUS_ As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                STATUS = 1
            Else
                STATUS = 0
            End If
        End Set
    End Property

    Property selectMasformula_ As String
        Get
            Return selectMasformula
        End Get
        Set(value As String)

        End Set
    End Property

    Property selectMasdformula_ As String
        Get
            Return selectMasdformula
        End Get
        Set(value As String)

        End Set
    End Property

    Public Sub insertMasformulaData()
        If connect.GetTable("SELECT fmid FROM masformula WHERE fmname = '" & FMNAME & "'").Rows.Count > 0 Then
            MsgBox("มีข้อมูลชื่อสูตรการคำนวณราคานี้อยู่ในระบบแล้ว")
        Else
            Try
                connect = CONDB.NewConnection()
                'connectlocal = conlocal.NewConnection()
                'Dim dischargeID As Integer

                connect.ExecuteNonQuery("INSERT INTO masformula (fmname, status) VALUES ('" & FMNAME & "', '" & STATUS & "')")
                'dischargeID = connect.ExecuteScalar("INSERT INTO masdischarge (DISCHARGE, STATUS) VALUES ('" & masdischargeDescription & "', '" & masdischargeStatus & "'); SELECT LAST_INSERT_ID();")
                'connectlocal.ExecuteNonQuery("INSERT INTO MASDISCHARGE (DISCHGID, DISCHARGE, STATUS) VALUES ('" & dischargeID & "', '" & masdischargeDescription & "', '" & masdischargeStatus & "')")
                MsgBox("ท่านได้ทำการเพิ่มข้อมูลสูตรการคำนวณราคาเรียบร้อยแล้ว")
            Catch ex As Exception
                MsgBox(vbCrLf & ex.Message)
            End Try
            connect.Dispose()
            'connectlocal.Dispose()
        End If
    End Sub

    Public Sub editMasformulaData()
        Try
            connect = CONDB.NewConnection()
            'connectlocal = conlocal.NewConnection()

            connect.ExecuteNonQuery("UPDATE masformula SET fmname = '" & FMNAME & "', status = '" & STATUS & "' WHERE fmid = '" & FMID & "'")
            'connectlocal.ExecuteNonQuery("UPDATE MASDISCHARGE SET DISCHARGE = '" & masdischargeDescription & "', STATUS = '" & masdischargeStatus & "' WHERE DISCHGID = '" & masdischargeID & "'")
            MsgBox("ท่านได้ทำการแก้ไขข้อมูลชื่อสูตรการคำนวณราคาเรียบร้อยแล้ว")
        Catch ex As Exception
            MsgBox(vbCrLf & ex.Message)
        End Try
        connect.Dispose()
        'connectlocal.Dispose()
    End Sub

    Public Sub insertMasdformulaData()
        Try
            connect = CONDB.NewConnection()
            'connectlocal = conlocal.NewConnection()
            'Dim dischargeID As Integer

            connect.ExecuteNonQuery("INSERT INTO masdformula (fmid, l_margin, h_margin, formula, status) VALUES ('" & FMID & "', '" & L_MARGIN & "', '" & H_MARGIN & "', '" & FORMULA & "', '" & STATUS & "')")
            'dischargeID = connect.ExecuteScalar("INSERT INTO masdischarge (DISCHARGE, STATUS) VALUES ('" & masdischargeDescription & "', '" & masdischargeStatus & "'); SELECT LAST_INSERT_ID();")
            'connectlocal.ExecuteNonQuery("INSERT INTO MASDISCHARGE (DISCHGID, DISCHARGE, STATUS) VALUES ('" & dischargeID & "', '" & masdischargeDescription & "', '" & masdischargeStatus & "')")
            MsgBox("ท่านได้ทำการเพิ่มข้อมูลสูตรการคำนวณราคาเรียบร้อยแล้ว")
        Catch ex As Exception
            MsgBox(vbCrLf & ex.Message)
        End Try
        connect.Dispose()
        'connectlocal.Dispose()
    End Sub

    Public Sub editMasdformulaData()
        Try
            connect = CONDB.NewConnection()
            'connectlocal = conlocal.NewConnection()
            'Dim dischargeID As Integer

            connect.ExecuteNonQuery("UPDATE masdformula SET fmid = '" & FMID & "', l_margin = '" & L_MARGIN & "', h_margin = '" & H_MARGIN & "', formula = '" & FORMULA & "', status = '" & STATUS & "' WHERE dfmid = '" & DFMID & "'")
            'dischargeID = connect.ExecuteScalar("INSERT INTO masdischarge (DISCHARGE, STATUS) VALUES ('" & masdischargeDescription & "', '" & masdischargeStatus & "'); SELECT LAST_INSERT_ID();")
            'connectlocal.ExecuteNonQuery("INSERT INTO MASDISCHARGE (DISCHGID, DISCHARGE, STATUS) VALUES ('" & dischargeID & "', '" & masdischargeDescription & "', '" & masdischargeStatus & "')")
            MsgBox("ท่านได้ทำการแก้ไขข้อมูลสูตรการคำนวณราคาเรียบร้อยแล้ว")
        Catch ex As Exception
            MsgBox(vbCrLf & ex.Message)
        End Try
        connect.Dispose()
        'connectlocal.Dispose()
    End Sub
End Class
